<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Post;
class UserController extends Controller
{
    public  function setting(){

    }
    public function settingStore(){

    }

    public function  show() {
        //个人信息，文章数，关注数，粉丝数量
        $id = request('id');
        $user = User::with(['posts','fans','stars'])->withCount(['posts','fans','stars'])
                ->find($id);
        //个人文章
        $posts = Post::orderBy('created_at','desc')->where('user_id',$id)->take(5)->get();
        //我的粉丝用户，包含粉丝用户的 关注/粉丝/ 文章数
        $fans = $user->fans();
        $fusers = User::whereIn('id',$fans->pluck('fan_id'))->withCount(['posts','fans','stars'])->get();
        //我的关注用户，包含关注用户的 关注/粉丝/ 文章数
        $stars = $user->stars();
        $susers = User::whereIn('id',$stars->pluck('star_id'))->withCount(['posts','fans','stars'])->get();
        return view('user.show',compact('user','posts','fusers','susers'));
    }

    public function  fan($id) {
        $me = \Auth::user();
        $me->doFan($id);
        return [
            'error' => 0,
            'msg' => ''
        ];
    }

    public function  unfan ($id) {
        $me = \Auth::user();
        $me->doUnFan($id);
        return [
            'error' => 0,
            'msg' => ''
        ];
    }
}
